<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="活动主题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['activityTheme', validatorRules.activityTheme]" placeholder="请输入活动主题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="举办方" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['organizer']" placeholder="请输入举办方"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="征稿开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择征稿开始时间" v-decorator="['startTime', validatorRules.startTime]" :trigger-change="true"
                style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="征稿结束时间(投稿开始时间)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择征稿结束时间(投稿开始时间)" v-decorator="['endTime', validatorRules.endTime]" :trigger-change="true"
                style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="投稿结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择投稿结束时间" v-decorator="['likeEndTime', validatorRules.likeEndTime]" :trigger-change="true"
                style="width: 100%" />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="活动海报" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['activityImg']" placeholder="请输入活动海报"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-item label="活动海报" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload class="avatar-uploader" text="上传" v-model="fileList" v-decorator="['activityImg']"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="活动说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['eventDescription']" placeholder="请输入活动说明"></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="活动内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['activities']" placeholder="请输入活动内容"></a-textarea>
            </a-form-item>
          </a-col>
          
          <a-col :span="24">
            <a-form-item label="规则说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['ruleDescription']" placeholder="请输入规则说明"></a-textarea>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="状态:1显示，2隐藏" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['type', validatorRules.type]" placeholder="请输入状态:1显示，2隐藏"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="删除：1正常，2已删除" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['delFlag', validatorRules.delFlag]" placeholder="请输入删除：1正常，2已删除"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import {
    validateDuplicateValue
  } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JImageUpload from '../../../components/jeecg/JImageUpload'

  export default {
    name: "OfficialCallForm",
    components: {
      JFormContainer,
      JDate,
      JImageUpload,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      normal: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        model: {},
        fileList: [],
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        confirmLoading: false,
        validatorRules: {
          activityTheme: {
            rules: [{
              required: true,
              message: '请输入活动主题!'
            }, ]
          },
          startTime: {
            rules: [{
              required: true,
              message: '请选择报名开始时间!'
            }, ]
          },
          endTime: {
            rules: [{
              required: true,
              message: '请选择报名结束时间!'
            }, ]
          },
          likeEndTime: {
            rules: [{
              required: true,
              message: '请选择投稿结束时间!'
            }, ]
          },
          type: {
            rules: [{
              required: true,
              message: '请输入状态:1显示，2隐藏!'
            }, ]
          },
          delFlag: {
            rules: [{
              required: true,
              message: '请输入删除：1正常，2已删除!'
            }, ]
          },
        },
        url: {
          add: "/officialCall/officialCall/add",
          edit: "/officialCall/officialCall/edit",
          queryById: "/officialCall/officialCall/queryById"
        }
      }
    },
    computed: {
      formDisabled() {
        if (this.normal === false) {
          if (this.formData.disabled === false) {
            return false
          } else {
            return true
          }
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.normal === false) {
          if (this.formData.disabled === false) {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'activityTheme', 'organizer', 'startTime', 'endTime',
            'activityImg', 'eventDescription', 'activities', 'type', 'delFlag', 'ruleDescription','likeEndTime'))
        })
      },
      //渲染流程表单数据
      showFlowData() {
        if (this.normal === false) {
          let params = {
            id: this.formData.dataId
          };
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result);
            }
          });
        }
      },
      submitForm() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'activityTheme', 'organizer', 'startTime', 'endTime', 'activityImg',
          'eventDescription', 'activities', 'type', 'delFlag', 'ruleDescription', 'likeEndTime'))
      },
    }
  }
</script>
